// JavaScript Document

//p = new Player(level.spawnPos.x, level.spawnPos.y);
//head.ready(loadingReady());

function loadingReady()  {
	var map = content.mapFiles[content.mapFiles.length-1];
	if (map) {
		content.level.name = map.name;
		content.level.spawnPos = map.spawnPos;
		game.p.setPos(content.level.spawnPos.x, content.level.spawnPos.y);
		setupWorld(map.map);
	}
	content.mapLoaded = true;
}

function setupWorld(map) {
	for (i = 0; i < map.length; i += 1) {
		content.level.map[i] = map[i].split(",");
	}
	content.level.map = rows2cols(content.level.map);
}

/**
 * This is a transpose function. Changes lines to columns and contrary
 *
 * @param map array [[a1, a2, ...], [a1, a2, ...], ...]
 * @return array [[a1, b1, ...], [a2, b2, ...], ...]
 */
function rows2cols(map) {
    var r = [];
    var t;
    for (var i = 0, iLen = map.length; i < iLen; i++) {
        t = map[i];
        for (var j = 0, jLen = t.length; j < jLen; j++) {
            if (!r[j]) {
                r[j] = [];
            }
            r[j][i] = t[j];
        }
    }
    return r;
}